package cn.itcast.service.company.impl;

import cn.itcast.dao.company.CompanyDao;
import cn.itcast.domain.company.Company;
import cn.itcast.service.company.CompanyService;
import cn.itcast.vo.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyDao companyDao;

    public List<Company> findAll() {
        return companyDao.findAll();
    }

    //保存
    @Override
    public void save(Company company) {
        company.setId(UUID.randomUUID().toString());
        companyDao.save(company);
    }

    //更新
    @Override
    public void update(Company company) {
        companyDao.update(company);
    }

    //根据id查询
    @Override
    public Company findById(String id) {
        return companyDao.findById(id);
    }

    //根据id删除
    @Override
    public void delete(String id) {
        companyDao.delete(id);
    }

    /**传统分页
     * page  :1,2,3,4,5
     * size:5
     *
     * SELECT * FROM ss_company WHERE is_delete=0 LIMIT 10,5
     */
    public PageResult findPage(int page, int size) {
        long total = companyDao.findCount();
        List list = companyDao.findPage((page-1) * size,size);

        return new PageResult(total,list, page, size);
    }
    /**
     * 通过PageHepler分页
     *  1、设置分页参数
     *  2、调用dao查询全部
     *  3、构造返回值
     * * 顺序一定不能写错，设置了分页参数之后，自动对第二步查询全部的sql语句进行分页
     * * 对第一个查询进行分页处理
     */
    @Override
    public PageInfo findPageByHelper(int page, int size) {
        PageHelper.startPage(page,size);
        List<Company> list = companyDao.findAll();
        return new PageInfo(list);
    }
}
